<cfsetting enablecfoutputonly="true">
<!--- Make sure all content to be outputed is wrapped in <cfoutput></cfoutput> tags. --->
<cfswitch expression="#thisTag.executionMode#">
	<cfcase value="start">
		<cfparam name="ATTRIBUTES.id" default="">
		<cfparam name="ATTRIBUTES.name" default="">
		<cfparam name="ATTRIBUTES.data" default="">
		<cfparam name="ATTRIBUTES.label" default="">
		<cfparam name="ATTRIBUTES.labelClass" default="coopFieldLabel">
		<cfparam name="ATTRIBUTES.wrapElement" default="ul">
		<cfparam name="ATTRIBUTES.delimiters" default=",">
		<cfparam name="ATTRIBUTES.module" default="">
		<cfparam name="ATTRIBUTES.modulePath" default="">
		<cfparam name="ATTRIBUTES.include" default="">
		<cfparam name="ATTRIBUTES.includePath" default="">
		<cfparam name="ATTRIBUTES.listItems" default="">
		<cfparam name="ATTRIBUTES.moduleSet" default="">
		<cfparam name="ATTRIBUTES.runEmpty" default="false">
		<cfif StructKeyExists(caller,'root')>
			<cfset root = caller.root>
		<cfelse>
			<cfset root = caller>
		</cfif>
		<cfif NOT thisTag.hasEndTag>
			<cfthrow detail="End tag required" message="All coop tags require an end tag." errorcode="sos.tag.attributes">
		</cfif>
		<cfif ATTRIBUTES.id EQ ''>
			<cfthrow detail="Missing required attribute: 'id'" message="'id' attribute is required">
		</cfif>
		<cfif !len(ATTRIBUTES.modulePath)>
			<cfif len(ATTRIBUTES.moduleSet)>
				<cfset ATTRIBUTES.modulePath = caller._api[ATTRIBUTES.moduleSet].get_modulePath()>
			<cfelse>
				<cfthrow detail="You must either declare a path for the module location or a module set if your framework supports this." message="either a path or module set must be delcared" type="coop.tag"/>
			</cfif>
		</cfif> 
        
		<cfscript>
		standardattributes = 'id,name,data,delimiters,label,labelClass,listItems,wrapElement,renderModule,renderPath,runEmpty,moduleSet'; // internally handled attributes
		attributes = root.coop.mergeattributes(attributes,root);
		attributeList = root.coop.createAttributeList(attributes,standardAttributes,root);
		if (ATTRIBUTES.name EQ '') {
			ATTRIBUTES.name = ATTRIBUTES.id;
		}
		
		if(len(ATTRIBUTES.include)){
			renderMode = "include";
			renderSet = reMatchNoCase("^@.[^@]+@",ATTRIBUTES.include);
			if(arraylen(renderSet) && structKeyExists(root,"_api")){
				renderKey = replace(renderSet[1],"@","","ALL");
				ATTRIBUTES.include = replace(ATTRIBUTES.include,renderSet[1],"");
				if(!len(ATTRIBUTES.include)){
					ATTRIBUTES.includePath = root._api[renderKey].get_includePath();
				}
			}
			if (ATTRIBUTES.includePath EQ '') {
				ATTRIBUTES.includePath = root.coop.get_sharePath() & 'tags/coop/includes/';
			}
		} else if(len(ATTRIBUTES.module)){
			renderMode = "module";
			renderSet = reMatchNoCase("^@.[^@]+@",ATTRIBUTES.module);
			if(arraylen(renderSet) && structKeyExists(root,"_api")){
				renderKey = replace(renderSet[1],"@","","ALL");
				ATTRIBUTES.module = replace(ATTRIBUTES.module,renderSet[1],"");
				if(!len(ATTRIBUTES.modulePath)){
					ATTRIBUTES.modulePath = root._api[renderKey].get_modulePath();
				}
			}
			if (ATTRIBUTES.modulePath EQ '') {
				ATTRIBUTES.modulePath = root.coop.get_sharePath() & 'tags/coop/renderers/';
			}
		} else {
			// need code to raise exception if neither is set.
			renderMode = "unknown";
		}
		</cfscript>
	</cfcase>
	<cfcase value="end">
		<!--- Head Content --->
		<!--- This is where you include any content that will be included in the head of your document --->
		<cfsaveContent variable="myJSScriptBlock">
			<!--- Include your javascript here and make sure it is wrapped with the following code: <cfoutput><script></script></cfoutput> --->
		</cfsaveContent>
		<cfsaveContent variable="myCSSBlock">
			<!--- Include your CSS here and make sure it is wrapped with the following code: <cfoutput><style></style></cfoutput> --->
		</cfsaveContent>
		<cfscript>
		/* *** Sample Entries ***
		root.coop.sosContent.addHeadContent(type:"js",src:"path/to/js file");
		root.coop.sosContent.addHeadContent(type:"js",content:"#myJSScriptBlock#");
		root.coop.sosContent.addHeadContent(type:"css",src:"path/to/CSS file");
		root.coop.sosContent.addHeadContent(type:"css",content:"#myCSSBlock#");
		 */
		</cfscript>
		<!--- pre content markup / code --->
		<cfscript>
		// Check to see if data is simple value and allow it to be passed in as generated content
		if (isSimpleValue(ATTRIBUTES.data) AND ATTRIBUTES.data EQ ''){
			ATTRIBUTES.data = thisTag.generatedContent;
			thisTag.generatedContent = '';
		}
		// Initialize the returnData object that we use for data handling
		returnData = createObject("component","#root.coop.get_shareClassPath()#objects.coop.returnData").init(ATTRIBUTES.data,ATTRIBUTES.delimiters);
		
		// get the number of records in the data passed into the returnData object
		recordCount = returnData.recordCount();
		// Remove any externally generated content.
		thisTag.generatedContent = '';
		
		// optional : start markup / code (outside data loop)
		if (ATTRIBUTES.label NEQ '') {
			writeOutput('<label class="#ATTRIBUTES.labelClass#">#ATTRIBUTES.label#</label>');
		}
		if(ATTRIBUTES.wrapElement NEQ "")
			writeOutput('<#ATTRIBUTES.wrapElement# id="#ATTRIBUTES.id#" name="#ATTRIBUTES.name#"#attributeList#>');
		// Loop through your data
		data = structNew();
		</cfscript>
		<cfloop from="1" to="#recordCount#" index="iRow">
			<cfscript>
			currentRow = returnData.returnDataRowStruct(iRow);
			data = currentRow;
			data.__meta__ = {
				start = iif(iRow==1,"true","false"),
				end = iif(iRow==recordCount,"true","false"),
				listItems = ATTRIBUTES.listItems,
				item = iRow,
				recordCount = recordCount
			};	
			</cfscript>
	        <cfoutput><cfmodule template="#ATTRIBUTES.modulePath##ATTRIBUTES.module#.cfm" attributeCollection="#data#"></cfoutput>
        </cfloop>
        <cfif ATTRIBUTES.runEmpty>
        	<cfscript>
			data = {
				__meta__ = {
				start = true,
				end = true,
				listItems = ATTRIBUTES.listItems,
				item = 0,
				recordCount = recordCount
				}
			};
			</cfscript>
        	<cfoutput><cfif renderMode EQ "module"><cfmodule template="#ATTRIBUTES.modulePath##ATTRIBUTES.module#.cfm" attributeCollection="#data#"><cfelseif rendermode EQ "include"><cfinclude template="#ATTRIBUTES.includePath##ATTRIBUTES.include#"></cfif></cfoutput>
        </cfif>
		<!--- for(iRow=1; iRow LTE recordCount; iRow = iRow + 1) {
			currentRow = returnData.returnDataRowStruct(iRow);
			writeOutput(ATTRIBUTES.renderObject.renderRow(currentRow));
		} --->
		<cfscript>
		// optional : end markup / code (outside data loop)
		if(ATTRIBUTES.wrapElement NEQ "")
			writeOutput('</#ATTRIBUTES.wrapElement#>');
		</cfscript>
		<!--- pre content markup / code -->
		<cfoutput>#thisTag.generatedContent#</cfoutput>
		<!-- post content markup / code --->
	</cfcase>
</cfswitch>
<cfsetting enablecfoutputonly="false">